<?php
namespace app\hongyanche\controller;
class Test{
	function user(){
		dump(session('from'));
		// session('from',null);
		die();
		dump(session('user'));
		dump($_SESSION);
	}
	function index(){
		$detail = array(
			'6' => array('20170406,afternoon','20170407,allday'),
			'7' => array(),
			'8' => array(),
			'9' => array(),
			);
		dump($detail);
		die();
		$detail = ['1152'=>12];
		$book = ['2531'=>2];
		return json([$detail,$book]);
		return;
		dump(session('user'));
		// session_start();
		// dump($_SESSION);
		// session_destroy();
	}
	function getlist(){
		$list = ['state'=>rand(0,5),'errormsg'=>'[errormsg]','data'=>['more'=>rand(0,3)]];
		while(rand(1,5)>1){
			$list['data']['list'][] = ['id'=>rand(0000,9999)];
		}
		return $list;
	}
	function cafe(){
		$list[] = [
			'cafe_id'	=> 500,
			'cafe_name'	=> 'cafe_name',
			'imgurls'	=> '1,2,3',
			'labels'	=> '798,804'
		];
		$res = ['state'=>1,'data'=>['list'=>$list]];
		return $res;
	}
	function rank(){
		$list = ['state'=>rand(0,5),'errormsg'=>'[errormsg]','data'=>['more'=>rand(0,3)]];
		$i = 0;
		$t = rand(0,8);
		while($i<$t){
			$range = [];
			while(rand(0,4)<3){
				$range[] = ['a'=>'1','b'=>'2'];
			}
			$list['data']['rank'][] = ['id'=>rand(0000,9999),'range'=>$range,'labels'=>$range];
			$i++ ;
		}
		return json($list);
	}
	function info(){
		$list = ['state'=>rand(0,5),'errormsg'=>'[errormsg]','info'=>[]];
		// while(rand(1,5)>1){
			// $list['data']['list'][] = [];
		// }
		return $list;
	}
	function email(){
		$code = 123;
		$auto = false;
		$email = 'bach109109@qq.com';
		$sendmail = model('Email');
		$sendmail = $sendmail->send( 
			$demand = "login",
			$data = array( 
				'code'=>$code,
				'auto'=>$auto?"?auto=1":"",
				'email'=>$email
				)
			) ;
		return json($sendmail);
	}
    function test(){
        header("Content-type:text/html;charset=utf-8");
        $upload = model('upload')->uploadFiles();
        $str = md5(uniqid(microtime(true),true));
        $file = 'upload/'.$str.'.mp4';
        exec("/usr/local/ffmpeg/ffmpeg-2.1.1/ffmpeg -i $upload -c:v libx264 -strict -2 $file",$a);
        if(!$a){
            //删除源文件，减少占用空间
            unlink($upload);
            //加入数据库
            return json(['state'=>1,'data'=>['url'=>'http://hy.tiesh.com/'.$file]]);
        }
    }
    function getip(){
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $cip = $_SERVER["HTTP_CLIENT_IP"];
        }
        else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        else if(!empty($_SERVER["REMOTE_ADDR"])){
            $cip = $_SERVER["REMOTE_ADDR"];
        }
        else{
            $cip = '';
        }
        preg_match("/[\d\.]{7,15}/", $cip, $cips);
        $cip = isset($cips[0]) ? $cips[0] : 'unknown';
        unset($cips);
        return $cip;
    }


    public function test2()
    {
        $upload = '/alidata/tp5/public/upload/1779701aefa356a0fffd1d86eb60445b.mp4';
        $file = '/alidata/tp5/public/upload/1.mp4';
        exec("/usr/local/h264/ffmpeg-2.1.1/ffmpeg -i /$upload -c:v libx264 -strict -2 $file", $a,$b);
        dump($a);
        dump($b);
    }

    public function test3()
    {
        $url = 'http://hy.tiesh.com/forum/aaaa?label_id=1';
        $params = substr($url,strpos($url,'=')+1);
        echo $params;
    }

    //模板消息
    public function wechat()
    {
        $data = [
            'touser' => 'ohnDo1L6cJ6aijiYJFSp2d__wbGs',
            'template_id' => 'CQCbbUOgjpVhu4MyO3UafE8B2sRJuOK24tNNaDGO0rs',
            'url' => 'http://hy.tiesh.com/home?order=1#my',
            'data' => [
                'name' => [
                    'value' => '大卡车',
                    'color' => '#173177',
                ],
                'remark' => [
                    'value' => '买的大卡车',
                    'color' => '#173177',
                ]
            ]
        ];

        $access_token = 'zPePALDrOhznMJtlP1p0IVJ6H3P1eZ2KB1hHWKu5uYbAF4D4Y0e9lIxLShgRDGUtLsb0mgRPEfqG0JlGs-gkCsovmrI25IQPdMpPzPxuevaPbHWXw9kBuPhNauQaCIHOVXZdAAAOQS';
        $params = json_encode($data,JSON_UNESCAPED_UNICODE);
        $start_time = microtime(true);
            $fp = fsockopen('api.weixin.qq.com', 80, $error, $errstr, 1);
            $http = "POST /cgi-bin/message/template/send?access_token={$access_token} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
            fwrite($fp, $http);
            fclose($fp);
        print_r(microtime(true) - $start_time);
    }

    public function test1()
    {
        $res = file_get_contents("http://192.168.248.1:8080/pubapi/vehicle/v1/get?vhcle=GD321068&key=837f373a-23ac-4607-a2f5-b029084841af");
//        $res = '{"message":"success","extend":{"vehicle":{"vhcle":"0070231880","vhvin":null,"vhvin8":"GD321068","deviceNo":"64696253987","simNo":"64696253987","license":null}}}';
     dump($res);
        if(!$res) return ['state'=>0,'errormsg'=>'数据错误'];
    }

    public function test4(User $user)
    {
        dump($user);
    }


    public function mingpian()
    {
        header('Content-type: image/jpeg');
//        die;
        // 由文件或 URL 创建一个新图象，这里的text.jpg就是效果1显示的预设图片
        $rImg = ImageCreateFromJPEG("http://hy.tiesh.com/_hongyanche/material/mpbg.jpg");

        //2.获得源文件的宽高
        $fx = imagesx($rImg); // 获取宽度
        $fy = imagesy($rImg); // 获取高度

        //3.使用固定的公式计算新的宽高
        $sx = 650;
        $sy = 300;
        //4.生成目标图像资源
        $small = imagecreatetruecolor($sx,$sy);


        //5.进行缩放
        imagecopyresampled($small,$rImg,0,0,0,0,$sx,$sy,$fx,$fy);

        $heder = ImageCreateFromJPEG('http://hy.tiesh.com/link/idphoto/WnBg66hpgk');
        $erweima = ImageCreateFromPNG('./_hongyanche/material/user.png');
        //2.获得源文件的宽高
        $efx = imagesx($erweima); // 获取宽度
        $efy = imagesy($erweima); // 获取高度

        //3.使用固定的公式计算新的宽高
        $esx = 100;
        $esy = 100;
        //4.生成目标图像资源
        $smaller = imagecreatetruecolor($esx,$esy);
        //5.进行缩放
        imagecopyresampled($smaller,$erweima,0,0,0,0,$esx,$esy,$efx,$efy);

        list($src_w, $src_h) = getimagesize('http://hy.tiesh.com/link/idphoto/WnBg66hpgk');
//        list($sr_w, $sr_h) = getimagesize('http://hy.tiesh.com/link/idphoto/WnBg66hpgk');
        imagecopymerge($small,$heder, 70, 56, 0, 0, $src_w, $src_h, 100);
        imagecopymerge($small,$smaller, 550, 0, 0, 0, 100, 100, 100);
    // 为一幅图像分配颜色
//        $cor = imagecolorallocate($rImg, 255, 255, 255);
    // 我们可以自定义与图片结合的文字字体
        $font = "./fonts/simhei.ttf";
    // 解码main.php里img标签已编码的URL字符串，包括first, last以及location
//        $first = urldecode('夏');
        $first = '夏浩文';
        $location = '重庆市渝北区宝圣大道';

        /*
        * 取得需要向图片上添加的文本的范围。imagettfbbox() 返回一个含有8个
        * 单元的数组表示了文本外框的四个角。
        */

        $blue = imagecolorallocate($small,0,0,0);
        imagettftext($small,25,0,200,100,$blue,$font,'姓名:');
        imagettftext($small,25,0,300,100,$blue,$font,$first);
        imagettftext($small,25,0,200,170,$blue,$font,'会员编号:');
        imagettftext($small,25,0,370,170,$blue,$font,'23');
        imagettftext($small,25,0,70,250,$blue,$font,'地址:');
        imagettftext($small,25,0,150,250,$blue,$font,$location);
        imagettftext($small,12,0,565,120,$blue,$font,'[扫我注册]');

//        $width_first = imagettfbbox(10, 0, $font, $first);
//        $width_last = imagettfbbox(10, 0, $font, $last);
//        $width_location = imagettfbbox(15, 0, $font, $location);

    // 进一法取整, $width_变量[2]代表右下角X坐标位置
//        $x_first = ceil($imgWidthSetting - $width_first[2]);
//        $x_last = ceil($imgWidthSetting - $width_last[2]);
//        $x_location = ceil($imgWidthSetting - $width_location[2]);

    // 利用imagettftext()函数将文本“写入”图像
//        imagettftext($rImg, 60, 0, $x_first, $textHeightOffset+120, $cor, $font, $first);
//        imagettftext($rImg, 70, 0, $x_last, $textHeightOffset+210, $cor, $font, $last);
//        imagettftext($rImg, 35, 0, $x_location, $textHeightOffset+290, $cor, $font, $location);
//        ob_clean();

        // 输出图象到浏览器或文件，quality 为可选项，范围从 0（最差质量，文件更小）到 100（最佳质量，文件最大）
        imagejpeg($small, null,100);
    // Free up memory, imagedestroy() 释放与 image 关联的内存。
        imagedestroy($small);
        die;
    }

    public function test5(){

        $str = '31';
        $zreo = '';
        for ($i = 0;$i<4-strlen($str);$i++){
            $zreo .= 0;
        }
//        echo $zreo.$str;
        echo (int)($zreo.$str);
}
}